﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace VectorTriangle
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            //控件随窗体全屏显示
            int count = this.Controls.Count * 2 + 2;
            float[] factor = new float[count];
            int i = 0;
            factor[i++] = Size.Width;
            factor[i++] = Size.Height;
            foreach (Control ctrl in this.Controls)
            {
                factor[i++] = ctrl.Location.X / (float)Size.Width;
                factor[i++] = ctrl.Location.Y / (float)Size.Height;
                ctrl.Tag = ctrl.Size;//!!!
            }
            Tag = factor;
        }
        Form2 form2 = new Form2();
        //点击按钮显示示意图，设置示意图宽高和窗体1一致
        private void button2_Click(object sender, EventArgs e)
        {
            
            form2.Width = this.Width;
            form2.Height = this.Height;
            form2.Show();
        }
        public void Form2(Form1 form1)
        {
            InitializeComponent();
            this.MdiParent = form1;
        }
        //设置窗体宽高的最小值
        private void Form1_ResizeEnd(object sender, EventArgs e)
        {
            if (this.Width <= 320)
            {
                this.Width = 320;
            }
            if (this.Height <= 320)
            {
                this.Height = 320;
            }
        }
        //计算OC,OD向量
        private void button1_Click(object sender, EventArgs e)
        {
            //判断文本框是否有值，获取值
            if (textBox1.Text != "" && textBox2.Text != "" && textBox3.Text != "" && textBox4.Text != "")
            {
                int m = Convert.ToInt32(textBox1.Text);
                int n = Convert.ToInt32(textBox2.Text);
                double VectorOA = Convert.ToDouble(textBox3.Text);
                double VectorOB = Convert.ToDouble(textBox4.Text);
                //计算向量AB
                double VectorAB = VectorOB - VectorOA;
                //计算向量AC,DB
                double VectorAC = VectorAB / m;
                double VectorDB = VectorAB / n;
                //计算向量OC,OD
                double VectorOC = VectorOA + VectorAC;
                double VectorOD = VectorOB - VectorDB;
                MessageBox.Show(string.Format("向量OC的值：{0}，向量OD的值：{1}", VectorOC, VectorOD), "结果");
            }
            else
            {
                MessageBox.Show("值不得为空!", "错误");
                return;
            }
        }
        //限制文本框只能输入数字
        private void LimitNumbers(KeyPressEventArgs e)
        {
            if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8)
            {
                e.Handled = true;
                MessageBox.Show("请输入0-9的数字!", "错误");
            }
        }
        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            LimitNumbers(e);
        }

        private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            LimitNumbers(e);
        }

        private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
        {
            LimitNumbers(e);
        }

        private void textBox4_KeyPress(object sender, KeyPressEventArgs e)
        {
            LimitNumbers(e);
        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            if (Tag != null)
            {
                //控件随窗体全屏显示
                float[] scale = (float[])Tag;
                int i = 2;
                foreach (Control ctrl in this.Controls)
                {
                    ctrl.Left = (int)(Size.Width * scale[i++]);
                    ctrl.Top = (int)(Size.Height * scale[i++]);
                    ctrl.Width = (int)(Size.Width / (float)scale[0] * ((Size)ctrl.Tag).Width);
                    ctrl.Height = (int)(Size.Height / (float)scale[1] * ((Size)ctrl.Tag).Height);
                    //每次使用的都是最初始的控件大小，保证准确无误。
                }
            }
        }
    }
}
